<?php

require_once('Config.php');

class AutoLoader {

    public static $loader;
	static $patterns = array();
	
    public static function init()
    {
        if (self::$loader == NULL)
            self::$loader = new self();

        return self::$loader;
    }

    public function __construct()
    {
        spl_autoload_register(array($this,'loader'));
        
        self::$patterns['/Controller/'] = 'application/controllers/';
        self::$patterns['/Model/'] = 'application/models/';
        self::$patterns['/Helper/'] = 'system/helpers/';   
    }

    public function loader($class)
    {
		foreach (self::$patterns as $key => $value) {
	        if (preg_match($key, $class)) {
	        	if (file_exists(Config::get('base_path') . $value . ucfirst($class) . '.php')) {
	        		require_once(Config::get('base_path') . $value . ucfirst($class) . '.php');
	        		return;
	        	}
	        }
		}
		if (file_exists(Config::get('base_path') . 'system/classes/' . ucfirst($class) . '.php')) {
        	require_once(Config::get('base_path') . 'system/classes/' . ucfirst($class) . '.php');
        	return;
        }
    }    
}
?>